package com.raos.example.webapi;

import com.raos.example.memory.entity.ChatRoom;
import com.raos.example.memory.service.ChatRoomService;
import com.raos.example.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天室业务服务实现类
 *
 * @author raos
 * @date 2025-07-14 20:58
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/openapi/chatroom")
public class OpenApiChatRoom {

    @Autowired
    private ChatRoomService chatRoomService;

    /**
     * 获取聊天室列表
     */
    @PostMapping("/list")
    public R<List<ChatRoom>> list(ChatRoom chatRoom) {
        List<ChatRoom> list = chatRoomService.list(chatRoom);
        return R.ok(list);
    }

    /**
     * 添加聊天室
     */
    @RequestMapping("/add")
    public R<String> add(ChatRoom chatRoom) {
        ChatRoom save = chatRoomService.add(chatRoom);
        log.info("save chatroom: {}", save);
        return R.ok(save.getRoomId());
    }

    /**
     * 修改聊天室
     */
    @RequestMapping("/update")
    public R<ChatRoom> update(ChatRoom chatRoom) {
        ChatRoom update = chatRoomService.update(chatRoom);
        log.info("update chatroom: {}", update);
        return R.ok(update);
    }

    /**
     * 删除聊天室
     */
    @RequestMapping("/delete")
    public R<T> delete(ChatRoom chatRoom) {
        log.info("delete chatroom: {}", chatRoom);
        chatRoomService.delete(chatRoom);
        return R.ok();
    }

}
